import { createRouter, createWebHashHistory } from 'vue-router'

// import Home from '../views/Home.vue'
// import Home from '../views/home/Home';
// import Login from '../views/login/Login';
// import Register from '../views/register/Register';
// import Shop from '../views/shop/Shop';

const routes = [
  /*{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/!* webpackChunkName: "about" *!/ '../views/About.vue')
  }*/

  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "Home" */'../views/home/Home.vue')
  },{
    path: '/Home',
    redirect: '/'
  },{
    path: '/cartList',
    name: 'CartList',
    component: () => import(/* webpackChunkName: "CartList" */'../views/cartList/CartList.vue')
  },{
    path: '/orderConfirmation/:id/:addressId?',
    name: 'OrderConfirmation',
    component: () => import(/* webpackChunkName: "OrderConfirmation" */'../views/orderConfirmation/OrderConfirmation.vue')
  },{
    path: '/orderList',
    name: 'OrderList',
    component: () => import(/* webpackChunkName: "OrderList" */'../views/orderList/OrderList.vue')
  },{
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "Login" */'../views/login/Login.vue'),

    //访问该路由之前才会执行(全局路由hook已放行,但还没有开始加载目标路由组件)
    beforeEnter(to, from, next){
      console.log('router/index.js /login 路由的 router hook');
      const isLogin = localStorage.getItem('isLogin');
      console.log('localStorage.isLogin:',localStorage.isLogin);

      //如果已经登录了 则直接跳转到首页
      if (isLogin) {
        console.log('已经登录过 重定向到首页');
        next({name: 'Home'})
      }else {
        next();
      }
    }
  },{
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "Register" */'../views/register/Register.vue')
  },{
    path: '/shop/:id',
    name: 'Shop',
    component: () => import(/* webpackChunkName: "Shop" */'../views/shop/Shop.vue')
  }, {
    path: '/person',
    name: 'PersonalInfo',
    component: () => import(/* webpackChunkName: "person" */ '../views/personalInfo/PersonalInfo'),
  }, {
    path: '/chooseAddressList/:shopId',
    name: 'ChooseAddressList',
    component: () => import(/* webpackChunkName: "chooseAddressList" */ '../views/chooseAddressList/ChooseAddressList'),
  }, {
    path: '/upsertAddress/:id?',
    name: 'UpsertAddress',
    component: () => import(/* webpackChunkName: "upsertAddress" */ '../views/upsertAddress/UpsertAddress'),
  },{
    path: '/myAddressList',
    name: 'MyAddressList',
    component: () => import(/* webpackChunkName: "myAddressList" */ '../views/myAddressList/MyAddressList'),
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//每次路由跳转前都会执行该方法
router.beforeEach((to, from, next) => {
  console.log('全局router hook');
  console.log('to:',to);
  console.log('from:',from);

  const isLogin = localStorage.getItem('isLogin');

  const isLoginOrRegister = to.name === 'Login' || to.name === 'Register';

  if (isLogin || isLoginOrRegister) {
    next();
  }else {
    next({name: 'Login'})
  }
});

export default router
